<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no" />
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css"/>
    <style>
        html, body {
            background-color: #f8f8f8;
        }
        .main ul li .li_box {
            height: 50px;
            line-height: 50px;
            display: -webkit-box;
            display: -webkit-flex;
            align-items: center;
            background-color: #fff;
            border-bottom: 1px solid #e8e8e8;
            padding: 0 15px;
        }
        .main ul li:last-child .li_box {
            border-bottom: none;
        }
        .main ul li .li_box .li_boxl {
            font-size: 15px;
            color: #333;
        }
        .main ul li .li_box .li_boxc {
        	height: 100%;
            color: #999;
            font-size: 12px;
            padding-left: 10px;
            flex: 1;
            -webkit-flex: 1;
            box-flex: 1;
            -webkit-box-flex: 1;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
            text-align: right;
        }
        .main ul li .li_box .li_boxc input {
        	width: 100%;
        	height: 100%;
        	color: #999;
        	text-align: right;
        	padding: 0 10px;
        	display: block;
        	box-sizing: border-box;
        }
        .main ul li .li_box .li_boxr img {
        	height: 13px;
        	display: block;
        	padding-left: 10px;
        }
        .main ul li .li_box .li_boxr .rm_span {
        	color: #e62442;
        	font-size: 12px;
        	display: block;
        }
        .main ul li .li_box .li_boxr .num_span {
        	color: #333;
        	font-size: 12px;
            display: block;
        }
        .main ul li .li_box .li_boxc #total_num::-webkit-input-placeholder{
            padding-top: 1px;
        }
        .main ul li .li_box .li_boxc #price::-webkit-input-placeholder{
            padding-top: 1px;
        }
    </style>
</head>
<body>
    <div class="main">
    	<ul>
            <li>
                <div class="li_box">
                    <div class="li_boxl">优惠券金额</div>
                    <div class="li_boxc"><input id="price" type="number" placeholder="请填写"></div>
                    <div class="li_boxr"><span class="rm_span">RM</span></div>
                </div>
            </li>
            <li onclick="fnSelectBeginTime();" tapmode>
                <div class="li_box">
                    <div class="li_boxl">开始时间</div>
                    <div id="begin_time" class="li_boxc">请选择</div>
                    <div class="li_boxr">
                    	<img src="../image/right.png">
                    </div>
                </div>
            </li>
            <li  onclick="fnSelectEndTime();" tapmode>
                <div class="li_box">
                    <div class="li_boxl">结束时间</div>
                    <div id="end_time" class="li_boxc">请选择</div>
                    <div class="li_boxr">
                    	<img src="../image/right.png">
                    </div>
                </div>
            </li>
            <li>
                <div class="li_box">
                    <div class="li_boxl">发送数量</div>
                    <div class="li_boxc"><input id="total_num" type="number" placeholder="请填写"></div>
                    <div class="li_boxr"><span class="num_span">张</span></div>
                </div>
            </li>
    	</ul>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/zepto.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript">
    apiready = function(){
        shop = $api.getStorage('shop_data');
        begin_time = '';
        end_time = '';

        api.addEventListener({
            name: 'addTicketEvent'
        }, function(ret, err) {
            if (ret) {
                var price = $('#price').val();
                var total_num = $('#total_num').val();
                if(!price) {
                    api.toast({
                        msg: '优惠金额不能为空',
                        location:'middle'
                    });
                    return;
                }
                if(!begin_time) {
                    api.toast({
                        msg: '开始时间不能为空',
                        location:'middle'
                    });
                    return;
                }
                if(!end_time) {
                    api.toast({
                        msg: '结束时间不能为空',
                        location:'middle'
                    });
                    return;
                }
                if(!total_num) {
                    api.toast({
                        msg: '发放数量不能为空',
                        location:'middle'
                    });
                    return;
                }
                openLoading();
                api.ajax({
                    url: rootPath+'/api/seller/add_ticket',
                    method: 'post',
                    data: {
                        values: { 
                            price: price,
                            begin_time : begin_time,
                            end_time : end_time,
                            total_num : total_num,
                            shop_id : shop.id,
                            token : shop.token
                        },
                    }
                }, function(ret, err) {
                    if (ret) {
                        if(ret.status == 200) {
                            api.toast({
                                msg: '添加成功',
                                location: 'middle'
                            });
                            api.sendEvent({
                                name: 'ticketChangeEvent',
                                extra: {
                                }
                            });

                            setTimeout(function(){
                                api.closeWin();
                            },1000);
                        }
                    } else {
                        alert(JSON.stringify(err.msg));
                    }
                    closeLoading();
                });
            }
        });
    };

    Date.prototype.Format = function (fmt) { //author: meizz 
        var o = {
            "M+": this.getMonth() + 1, //月份 
            "d+": this.getDate(), //日 
            "h+": this.getHours(), //小时 
            "m+": this.getMinutes(), //分 
            "s+": this.getSeconds(), //秒 
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
            "S": this.getMilliseconds() //毫秒 
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }

    // 打开日期换时间
    function fnSelectBeginTime () {
        $('input').blur();
        var date = new Date().Format("yyyy-MM-dd");
        var to_date = begin_time?begin_time:date;
        api.openPicker({
            type: 'date',
            date: to_date,
            title: '选择日期'
        }, function(ret, err) {
            if (ret) {
                if(10 > ret.month){
                    var month = '0' + ret.month;
                }else{
                    var month = ret.month;
                }
                if(10 > ret.day){
                    var day = '0' + ret.day;
                }else{
                    var day = ret.day;
                }
                begin_time = ret.year + '-' + month + '-' + day;
                $('#begin_time').text(begin_time);
            }
        });

    };
    function fnSelectEndTime () {
        if(!begin_time) {
            api.toast({
                msg: '请先选择开始时间',
                location:'middle'
            });
            return;
        }
        var date = new Date().Format("yyyy-MM-dd");
        var to_date = end_time?end_time:date;
        api.openPicker({
            type: 'date',
            date: to_date,
            title: '选择日期'
        }, function(ret, err) {
            if (ret) {
                if(10 > ret.month){
                    var month = '0' + ret.month;
                }else{
                    var month = ret.month;
                }
                if(10 > ret.day){
                    var day = '0' + ret.day;
                }else{
                    var day = ret.day;
                }
                time = ret.year + '-' + month + '-' + day;
                if(time <= begin_time) {
                    api.toast({
                        msg: '结束时间必须大于开始时间',
                        location : 'middle'
                    });
                    return;
                }
                end_time  = time;
                $('#end_time').text(end_time);
            }
        });

    };
</script>
</html>